package com.project.dao.imp;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class IUserDaoImp extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String userName, String passWord) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareCall("select * from r_user where r_userName =? and r_pwd = ?");
            ps.setObject(1, userName);
            ps.setObject(2, passWord);

            re = ps.executeQuery();
            if (re.next()) {
                user = new UserBean();
                user.setId(re.getInt("pk_userId"));
                user.setUserName(re.getString("r_userName"));
                user.setPassWord(re.getString("r_pwd"));
                user.setName(re.getString("r_name"));
                user.setGrade(re.getString("r_grade"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return user;
    }

    @Override
    public void updatePassWord(Integer id, String passWord) {
        this.setConnection();
        try {
            ps = con.prepareCall("update r_user set r_pwd = ?where pk_userId = ?");
            ps.setObject(1, passWord);
            ps.setObject(2, id);

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void addUser(UserBean userBean) {
        this.setConnection();
        try {
            ps = con.prepareCall("insert into r_user(r_userName,r_pwd,r_name,r_grade)" +
                    "VALUES(?,?,?,?)");
            ps.setObject(1, userBean.getUserName());
            ps.setObject(2, userBean.getPassWord());
            ps.setObject(3, userBean.getName());
            ps.setObject(4, userBean.getGrade());

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public boolean checkName(String userName) {
        this.setConnection();
        UserBean user = null;
        try {
            ps = con.prepareCall("select * from r_user where r_userName = ?");
            ps.setObject(1, userName);


            re = ps.executeQuery();
            if (re.next()) {
                user = new UserBean();
                user.setId(re.getInt("pk_userId"));
                user.setUserName(re.getString("r_userName"));
                user.setPassWord(re.getString("r_pwd"));
                user.setName(re.getString("r_name"));
                user.setGrade(re.getString("r_grade"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        if (user == null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<UserBean> findAll() {
        List<UserBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareCall("select * from r_user");

            re = ps.executeQuery();
            while (re.next()) {
                UserBean user = new UserBean();
                user.setId(re.getInt("pk_userId"));
                user.setUserName(re.getString("r_userName"));
                user.setPassWord(re.getString("r_pwd"));
                user.setName(re.getString("r_name"));
                user.setGrade(re.getString("r_grade"));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareCall("select * from r_user where pk_userId = ?");
            ps.setObject(1, id);

            re = ps.executeQuery();
            if (re.next()) {
                user = new UserBean();
                user.setId(re.getInt("pk_userId"));
                user.setUserName(re.getString("r_userName"));
                user.setPassWord(re.getString("r_pwd"));
                user.setName(re.getString("r_name"));
                user.setGrade(re.getString("r_grade"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        return user;
    }

    @Override
    public void delUser(Integer id) {
        this.setConnection();
        try {
            ps = con.prepareCall("delete from r_user where pk_userId = ?");
            ps.setObject(1, id);

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }

    @Override
    public void updateUserGrade(Integer id, String grade) {
        this.setConnection();
        try {
            ps = con.prepareCall("update r_user set r_grade = ? where pk_userId = ?");
            ps.setObject(1, grade);
            ps.setObject(2, id);

            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }
}
